package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class ChangeUserBaseInfoContract
{
    public ChangeUserBaseInfoContract() { }
    public ChangeUserBaseInfoContract(String userID, String name, String signature, String orgID)
    {
        this.UserID = userID;
        this.Name = name;
        this.Signature = signature;
        this.OrgID = orgID;
    }

    public String UserID ;
    public String Name ;
    public String Signature ;
    public String OrgID ;
    /// <summary>
    /// 当服务端向好友发送通知前，设置该字段为最新的版本号。
    /// </summary>
    public int UserLatestVersion ;

    public byte[] serialize() throws Exception {
        byte[] bName = Name.getBytes(StandardCharsets.UTF_8);
        byte[] bOrgID = OrgID.getBytes(StandardCharsets.UTF_8);
        byte[] bSignature = Signature.getBytes(StandardCharsets.UTF_8);
        byte[] bUserID = UserID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bName.length + 4 + bOrgID.length + 4 + bSignature.length + 4+bUserID.length+4;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bName.length);
        body.writeBytes(bName);
        body.writeInt(bOrgID.length);
        body.writeBytes(bOrgID);
        body.writeInt(bSignature.length);
        body.writeBytes(bSignature);
        body.writeInt(bUserID.length);
        body.writeBytes(bUserID);
        body.writeInt(UserLatestVersion);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.Name= SerializeUtils.readStrIntLen(buffer);
        this.OrgID=SerializeUtils.readStrIntLen(buffer);
        this.Signature=SerializeUtils.readStrIntLen(buffer);
        this.UserID=SerializeUtils.readStrIntLen(buffer);
        this.UserLatestVersion=buffer.readInt();
    }
}
